package com.youpingou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youpingou.domain.*;
import com.youpingou.mapper.*;
import com.youpingou.service.SkuCommentService;
import com.youpingou.service.SkuService;
import com.youpingou.vo.OneSkuResult;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private SkuCommentMapper skuCommentMapper;

    @Autowired
    private SkuPhotoMapper skuPhotoMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SpecificationMapper specificationMapper;

    @Autowired
    private SkuMapper skuMapper;

    public OneSkuResult findSkuBySkuId(Integer skuId){
        //1 创建返回值对象
        OneSkuResult result = new OneSkuResult();
        //组装根据skuid查找商品信息的数据
        //2. 查询sku信息
        Sku sku = baseMapper.selectById(skuId);
        //3. 查询spu信息
        Spu spu = spuMapper.selectById(sku.getSpuId());

        //4. skuid
        result.setSkuid(sku.getId());
        //5. spuid
        result.setSpuid(spu.getId());
        //6. goods_name
        result.setGoods_name(sku.getSkuName());
        //7. price
        result.setPrice(sku.getPrice());
        //8. on_sale_date
        result.setOn_sale_date(spu.getOnSaleTime());
        //9. 评论数量
        Integer commentCount = skuCommentMapper.findCountBySpuId(spu.getId());
        result.setComment_count(commentCount);

        //10. 评论级别
        Double comment_level = skuCommentMapper.findAvgStarBySpuId(spu.getId());
        result.setComment_level(comment_level.intValue());

        //11. cat_info
        result.setCat1_info(categoryMapper.selectById(spu.getCat1Id()));
        result.setCat2_info(categoryMapper.selectById(spu.getCat2Id()));
        result.setCat3_info(categoryMapper.selectById(spu.getCat3Id()));

        //12. 首图
        HashMap<String, String> logo = new HashMap<>();
        logo.put("smlogo",sku.getImages());
        logo.put("biglogo",sku.getImages());
        logo.put("xbiglogo",sku.getImages());
        result.setLogo(logo);


        //13. photos
        List<SkuPhoto> photos = skuPhotoMapper.findSkuPhotoBySkuId(sku.getId());
        List<Map> photoList = new ArrayList<>();

        // 遍历photos集合<String,String>
        for(SkuPhoto sp:photos){
            HashMap<String, String> map = new HashMap<>();
            map.put("smimg",sp.getUrl());
            map.put("bigimg",sp.getUrl());
            map.put("xbigimg",sp.getUrl());
            photoList.add(map);
        }
        // 将photoList插入result
        result.setPhotos(photoList);

        //14. 商品描述
        result.setDescription(spu.getDescription());
        //15. 售后
        result.setAftersale(spu.getAftersale());
        //16. 库存量
        result.setStock(sku.getStock());

        //17. 规格和规格选项----根据什么查找规格和规格选项
        //17.1 计算所有的规格id   1:1|2:6|6:22
        String[] sku_id_list_array = sku.getSkuIdList().split("\\|");
        //17.2 获取规格id   1    2   6
        List<Integer> idList = new ArrayList<>();
        for(String sku_id:sku_id_list_array){
            String[] ids = sku_id.split(":");
            idList.add(Integer.parseInt(ids[0]));
        }

        //17.2 根据规格id获取规格选项
        List<Specification> list = new ArrayList<>();
        for(Integer id:idList){
            // 查找Specification 以及 option
            list.add(specificationMapper.findSpecificationBySpecid(id));
        }

        //17.3 设置result
        result.setSpec_list(list);

        //18. 当前sku的排列组合
        Map<String,String> spec_info = new HashMap<>();
        spec_info.put("id_list",sku.getSkuIdList());
        spec_info.put("id_txt",sku.getSkuIdTxt().replace("{","").replace("}",""));
        result.setSpec_info(spec_info);


        //19. 所有的skus --通过skuid--spu--spu下的所有的skus
        // 根据spuid查找所有的skus
        List<Sku> skuList = skuMapper.findSkuListBySpuId(sku.getSpuId());

        // 准备集合
        List<Map<String, Object>> sku_list_map = new ArrayList<>();
        for(Sku s:skuList){
            HashMap<String, Object> map = new HashMap<>();
            map.put("skuid",s.getId());
            map.put("id_list",s.getSkuIdList().replace("{","").replace("}",""));
            sku_list_map.add(map);
        }
        result.setSku_list(sku_list_map);




        return result;
    }

    @Override
    public void updateSku(Integer skuid, Integer count) {
        Sku sku = baseMapper.selectById(skuid);
        sku.setStock(sku.getStock()-count);
        baseMapper.updateById(sku);
    }
}